{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conditional Statements: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's recall the example we have done in class last lecture:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What's yor name? Bob\n",
      "What's your surname? Smith\n",
      "That is Bob Smith\n"
     ]
    }
   ],
   "source": [
    "name = input(\"What's yor name? \")\n",
    "surname = input(\"What's your surname? \")\n",
    "print(\"That is\", name,surname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In programming term, that is a sequential structure, that is we have a set of statements that are executed in the given order. However, there are situations were this structure is not feasible, such as when we have to deal with a set of conditional statements. Hence, we have to resort to more complex structures, like the decision-making structure. <br>\n",
    "In Python, the if statement is used to implement a decision-making structure. The first line of this statement is called _if clause_: it starts with the _if keyword_ followed by a condition and ends with a colon. And then we have the body which contains a set of statements taht are executed only if the condition is true."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Remark**. If the stamente is false, then the body is not executed, and hence nothing happen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a name of a person you know: Anna\n",
      "I know Anna!\n"
     ]
    }
   ],
   "source": [
    "name = input('Enter a name of a person you know: ')\n",
    "if name == \"Anna\":\n",
    "    print('I know {}!'.format(name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can add an extra level in our conditional statement by using the _else clause_. The **if-else** statement enables to print a double alternative decision-making structure, which implies two possible execution paths (branches) depending on whether the condition is true or false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a name of a person you know: Michael\n",
      "I don't know that person\n"
     ]
    }
   ],
   "source": [
    "name = input('Enter a name of a person you know: ')\n",
    "if name == \"Anna\":\n",
    "    print('I know {}!'.format(name))\n",
    "else:\n",
    "    print(\"I don't know that person\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also nest different conditional statements, as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a name of a person you know: Mark\n",
      "I know Mark! That is my brother!\n"
     ]
    }
   ],
   "source": [
    "name = input('Enter a name of a person you know: ')\n",
    "if name == \"Anna\":\n",
    "    print('I know {}!'.format(name))\n",
    "else:\n",
    "    if name == \"Mark\":\n",
    "        print(\"I know {}! That is my brother!\".format(name))\n",
    "    else:\n",
    "        print(\"I don't know that person\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Logical Operators (AND - OR)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Suppose we are working in the marketing department of a national TV broadcaster, and we are interesting in making a marketing campaing that investigate our clients' Sunday habits! We would like to understand whether each of them is at home on Sunday, and if yes, understand the number of hours she watch the TV to propose a new sport offer.<br>\n",
    "To do so, we use extend the decision-making structure by using logical operators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Do you like sports? (y/n) y\n",
      "Are you typically at home on sunday? (y/n)n\n",
      "How many hours do you watch TV? 18\n",
      "You will get an offer from us!\n"
     ]
    }
   ],
   "source": [
    "hobby = input(\"Do you like sports? (y/n) \").lower()\n",
    "sunday = input(\"Are you typically at home on sunday? (y/n)\").lower()\n",
    "hours = int(input(\"How many hours do you watch TV? \"))\n",
    "\n",
    "if ((hobby in ['yes','y'])&(sunday in ['yes','y'])&(hours>6))|\\\n",
    "((hobby in ['yes','y'])&(sunday in ['no','n'])&(hours>14)):\n",
    "    print(\"You will get an offer from us!\")\n",
    "else:\n",
    "    print(\"That client is not profittable for us\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
